package environment;

import logic.Indy;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import es.deusto.ingenieria.is.xml.InformationXMLReader;

public class EnvironmentXMLReader extends InformationXMLReader {

	/*
	 * private int height; private int width;
	 */
	private Cell[][] grid;
	private Indy indy;

	private Cell chest;
	private Cell gem1;
	private Cell gem2;
	private Cell gem3;
	private Cell gem4;

	public EnvironmentXMLReader(String xmlFile) {
		super(xmlFile);
	}

	public Object getInformation() {
		Environment environment = new Environment(grid, indy, chest, gem1,
				gem2, gem3, gem4);
		environment.getIndy().setLocation(this.indy.getLocation());
		environment.setGoalAsNearestGem();
		return environment;
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		try {
			if (qName.equals("is:maze")) {
				grid = new Cell[Integer.parseInt(attributes.getValue("width"))][Integer
						.parseInt(attributes.getValue("height"))];

				for (int y = 0; y < this.grid.length; y++) {
					for (int x = 0; x < this.grid[y].length; x++) {
						this.grid[y][x] = new Cell();
						this.grid[y][x].y = y;
						this.grid[y][x].x = x;
						this.grid[y][x].setRight(true);
						this.grid[y][x].setDown(true);
						this.grid[y][x].setLeft(true);
						this.grid[y][x].setUp(true);
					}
				}

			} else if (qName.equals("is:start")) {
				this.indy = new Indy();
				Cell cellIndy = new Cell();			
				cellIndy.y = Integer.parseInt(attributes.getValue("row")) - 1;
				cellIndy.x = Integer.parseInt(attributes.getValue("column")) - 1;
				indy.setLocation(cellIndy);
			} else if (qName.equals("is:treasure")) {
				this.chest = new Cell();
				this.chest.y = Integer.parseInt(attributes.getValue("row")) - 1;
				this.chest.x = Integer.parseInt(attributes.getValue("column")) - 1;
			} else if (qName.equals("is:gemlocation")) {
				if (gem1 == null) {
					gem1 = new Cell();
					this.gem1.y = Integer.parseInt(attributes.getValue("row")) - 1;
					this.gem1.x = Integer.parseInt(attributes
							.getValue("column")) - 1;
				} else if (gem2 == null) {
					gem2 = new Cell();
					this.gem2.y = Integer.parseInt(attributes.getValue("row")) - 1;
					this.gem2.x = Integer.parseInt(attributes
							.getValue("column")) - 1;
				} else if (gem3 == null) {
					gem3 = new Cell();
					this.gem3.y = Integer.parseInt(attributes.getValue("row")) - 1;
					this.gem3.x = Integer.parseInt(attributes
							.getValue("column")) - 1;
				} else {
					gem4 = new Cell();
					this.gem4.y = Integer.parseInt(attributes.getValue("row")) - 1;
					this.gem4.x = Integer.parseInt(attributes
							.getValue("column")) - 1;
				}
			} else if (qName.equals("is:eastarrow")) {
				grid[Integer.parseInt(attributes.getValue("column")) - 1][Integer
						.parseInt(attributes.getValue("row")) - 1]
						.setRight(false);
			} else if (qName.equals("is:westarrow")) {
				grid[Integer.parseInt(attributes.getValue("column")) - 1][Integer
						.parseInt(attributes.getValue("row")) - 1]
						.setLeft(false);
			} else if (qName.equals("is:northarrow")) {
				grid[Integer.parseInt(attributes.getValue("column")) - 1][Integer
						.parseInt(attributes.getValue("row")) - 1]
						.setUp(false);
			} else if (qName.equals("is:southarrow")) {
				grid[Integer.parseInt(attributes.getValue("column")) - 1][Integer
						.parseInt(attributes.getValue("row")) - 1]
						.setDown(false);
			}
		} catch (Exception ex) {
			System.out.println(this.getClass().getName() + ".startElement(): "
					+ ex);
			ex.printStackTrace();
		}

	}

}
